package com.chzu.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chzu.pojo.StudentInfo;
import com.chzu.pojo.SysUser;
import com.chzu.pojo.vo.StudentInfoByOpenIdVo;
import com.chzu.result.JsonResult;
import com.chzu.service.StudentInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author lz
 * @Version 1.0
 * @Company 滁州大健康与养老产业研究院
 * @ClassName StudentInfoController
 * @Description TODO
 * @Date 2022/02/23 23:22
 */
@RestController
@RequestMapping("/studentInfo")
@Api(tags = "学生具体信息")
public class StudentInfoController {

    @Resource
    private StudentInfoService studentInfoService;

    @ApiOperation(("根据用户学号查询用户具体信息"))
    @PostMapping("/getStudentInfo/{userName}")
    public JsonResult<Object> getStudentInfoByUserName(@PathVariable("userName") String userName) {
        QueryWrapper<StudentInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("user_name",userName);
        StudentInfo data = studentInfoService.getOne(wrapper);
        return JsonResult.ok(data);
    }

    @ApiOperation(("根据微信用户唯一标识查询用户具体信息"))
    @GetMapping("/getStudentInfo")
    public JsonResult<Object> getStudentInfoByOpenId() {
        SysUser user = (SysUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String openId = user.getOpenId();
        if (openId == null) {
            return JsonResult.fail("用户信息获取失败，请联系管理员");
        }
        StudentInfoByOpenIdVo data = studentInfoService.getStudentInfoByOpenId(openId);
        if (data == null) {
            return JsonResult.fail("用户信息获取失败，请联系管理员");
        }
        return JsonResult.ok(data);
    }

    @ApiOperation(("根据微信用户唯一标识更新用户具体信息"))
    @PutMapping("/getStudentInfo")
    public JsonResult<String> updateStudentInfoByOpenId(@RequestBody StudentInfo studentInfo) {
        SysUser user = (SysUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String openId = user.getOpenId();
        if (openId == null) {
            return JsonResult.fail("用户信息获取失败，请联系管理员");
        }
        QueryWrapper<StudentInfo> wapper = new QueryWrapper<>();
        wapper.eq("open_id",openId);
        boolean res = studentInfoService.update(studentInfo, wapper);
        if (!res) {
            return JsonResult.fail("用户信息更新失败，请联系管理员");
        }
        return JsonResult.ok("用户信息更新成功");
    }
}
